<!--
Copyright © 2017, Che-Wei Hsu <cwxhsu@gmail.com>
This file is part of the MintCM.
Some rights reserved. See README.
-->

<html>

<head>

<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<script type="text/javascript" src="jquery_main.js"></script>
<script type="text/javascript" src="json3.js"></script>
<script type="text/javascript" src="mcm_jslib_api.js"></script>
<script type="text/javascript" src="mcm_jslib_data_info_auto.js"></script>
<script type="text/javascript" src="web_app_0405_script.js"></script>
<link rel="stylesheet" href="web_app_0405_style.css">

<script type="text/javascript">
var mcm_dv = null;

function body_init()
{
    var self_jslib, req_cmd = "", rep_ret;


    // 指定要讀出的資料.
    req_cmd = "&get.device.vap.*" +
              "&get.device.vap.*.station.*";

    // 送出讀出資料請求.
    self_jslib = new mcm_jslib_lib_t();
    self_jslib.socket_path = "@mintcm";
    self_jslib.session_permission = mcm_session_permission.ro;
    self_jslib.session_stack_size = 0;
    self_jslib.request_command = req_cmd;
    self_jslib.data_format = mcm_data_format.all_default;
    self_jslib.other_query = "";
    rep_ret = mcm_jslib_obtain_config(self_jslib);
    if(rep_ret.rep_code < mcm_return_code.pass)
    {
        alert("call mcm_jslib_obtain_config() fail" +
              "[" + rep_ret.rep_code + "]");
        mcm_jslib_run_script(rep_ret.rep_data);
        return;
    }
    // 處理資料格式.
    mcm_dv = JSON.parse(rep_ret.rep_data);

    // 每一筆 device.vap.{i} 建立一個表格.
    for(tmp_html = "", i = 0; i < mcm_dv.device.vap.length; i++)
        tmp_html += jsapi_make_empty_table("vap_list_" + i, "css_table1");
    $("#table_list").html(tmp_html);

    // 顯示表格的標題.
    for(i = 0; i < mcm_dv.device.vap.length; i++)
    {
        data_array = new Array(1);
        data_array[0] = "device.vap.@" + (i + 1);
        tmp_html = jsapi_make_trtd("css_td1", data_array);
        $("#vap_list_" + i).find("tbody").append(tmp_html);
    }

    // 將 device.vap.{i}.station.* 資料顯示在 device.vap.{i} 的表格中.
    for(i = 0; i < mcm_dv.device.vap.length; i++)
    {
        tmp_html = "";
        tmp_html += "<table class=\"css_table1\">"
        tmp_html += "<tr>";
        tmp_html += "<td class=\"css_td1\"></td>";
        tmp_html += "<td class=\"css_td1\">ekey</td>";
        tmp_html += "<td class=\"css_td1\">mac_addr</td>";
        tmp_html += "<td class=\"css_td1\">rule</td>";
        tmp_html += "<td class=\"css_td1\">add</td>";
        tmp_html += "</tr>";

        for(j = 0; j < mcm_mcount_device_vap_station_max_count; j++)
        {
            data_array = new Array(5);
            if(j < mcm_dv.device.vap[i].station.length)
            {
                // 顯示 device.vap.{i}.station.{j}
                data_array[0] = "device.vap.@" + (i + 1) + ".station.@" + (j + 1);
                data_array[1] = mcm_dv.device.vap[i].station[j].ekey;
                data_array[2] = mcm_dv.device.vap[i].station[j].mac_addr;
                data_array[3] = mcm_dv.device.vap[i].station[j].rule;
                data_array[4] = jsapi_make_checkbox("station_check_" + i + "_" + j, 1);
            }
            else
            {
                // 顯示空白表格.
                data_array[0] = "device.vap.@" + (i + 1) + ".station.*";
                data_array[1] = jsapi_make_text("ekey_text_" + i + "_" + j, 4, "");
                data_array[2] = jsapi_make_text("mac_addr_text_" + i + "_" + j, 20, "");
                data_array[3] = jsapi_make_text("rule_text_" + i + "_" + j, 6, "");
                data_array[4] = jsapi_make_checkbox("station_check_" + i + "_" + j, 0);
            }
            tmp_html += jsapi_make_trtd("css_td1", data_array);
        }

        tmp_html += "</table>";

        $("#vap_list_" + i).find("tbody").append(tmp_html);
    }
}

// 增加 device.vap.*.station.*
function config_add()
{
    var self_jslib, req_cmd = "", rep_ret, tmp_path, i, j;


    for(i = 0; i < mcm_dv.device.vap.length; i++)
        for(j = 0; j < mcm_mcount_device_vap_station_max_count; j++)
            if($("#station_check_" + i + "_" + j).prop("checked") == true)
            {
                // 填充 device.vap.{i}.station.{j} 路徑.
                tmp_path = "device" +
                           ".vap.#" + mcm_dv.device.vap[i].ekey +
                           ".station.#" + $("#ekey_text_" + i + "_" + j).val();

                // 增加 device.vap.{i}.station.{j}
                req_cmd += "&add." + tmp_path + "=";
                // 設定 device.vap.{i}.station.{j}.mac_addr
                req_cmd += "&set." + tmp_path + ".mac_addr=" +
                           mcm_jslib_convert_submit_str($("#mac_addr_text_" + i + "_" + j).val());
                // 設定 device.vap.{i}.station.{j}.rule
                req_cmd += "&set." + tmp_path + ".rule=" +
                           $("#rule_text_" + i + "_" + j).val();
            }

    // 執行內部模組.
    req_cmd += "&run.mcm_module_status_test_get_station";

    // 送出修改資料請求.
    self_jslib = new mcm_jslib_lib_t();
    self_jslib.socket_path = "@mintcm";
    self_jslib.session_permission = mcm_session_permission.rw;
    self_jslib.session_stack_size = 0;
    self_jslib.request_command = req_cmd;
    self_jslib.after_complete = mcm_after_complete.reload;
    self_jslib.other_query = "";
    rep_ret = mcm_jslib_submit_config(self_jslib);
    if(rep_ret.rep_code < mcm_return_code.pass)
    {
        alert("call mcm_jslib_submit_config() fail" +
              "[" + rep_ret.rep_code + "]");
        mcm_jslib_run_script(rep_ret.rep_data);
        return;
    }
    mcm_jslib_run_script(rep_ret.rep_data);
}
</script>

</head>

<body onload="body_init()">

  <div>[add][device.vap.*.station.*]</div>

  <div id="table_list"></div>

  <button type="button" onclick="config_add()">add</button>

</body>

</html>
